#version 300 es
precision highp float;

uniform sampler2D texGray;
uniform sampler2D texLut;
uniform int       chsMask;

in  vec2 grayCoord;
out vec4 fragColor;

void main()
{
    vec4 grayColor = texture(texGray, grayCoord);
    float ch = grayColor.r * 256.0 + 0.08;

    int chi = int(ch) & (chsMask << 1);
    if (chi == 0) { // channel disabled
        discard;
    }

    float light = clamp(grayColor.g, 0.0, 0.998);

    fragColor = vec4(0.0);
    for (int c = 1; c <= 4; ++c) {
        if ((chi & (1 << c)) != 0) {
            // ch #c have value
            if (fragColor != vec4(0.0))
                fragColor = mix(fragColor, texture(texLut, vec2(light, (float(c) + 0.08) / 6.0)), 0.9);
            else
                fragColor = texture(texLut, vec2(light, (float(c) + 0.05) / 6.0));
        }
    }

//    fragColor = texture(texLut, vec2(light, ch / 6.0));
//    fragColor = vec4(light, ch / 32.0, 0.0, 1.0);
}